﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using StockMatch.Service;
using StockMatch.Service.Entity;
using  Titan;
namespace StockMatch.WebSite.Controllers
{
    public class AdminHomeController : Controller
    {
        public ActionResult Index()
        {
            if (!string.IsNullOrEmpty(Request.QueryString["ReturnUrl"]))
            {
                ViewBag.ReturnUrl =Server.UrlDecode(Request.QueryString["ReturnUrl"]);
            }
            return View();
        }

        public ActionResult Login(string adminUserName, string adminPassword, string validateCode)
        {
            var result = new {data = 0, message = ""};

            int loginError = 0;
            if (!string.IsNullOrEmpty(Request.GetCookieValue("LoginError")))
            {
                loginError = Int32.Parse(Request.GetCookieValue("LoginError")) + 1;
            }
            else
            {
                Response.SetCookie("LoginError", 0);
            }

            if (string.IsNullOrEmpty(adminUserName))
            {
                result = new {data = 0, message = "用户名不能为空"};
                return Json(result);
            }
            if (string.IsNullOrEmpty(adminPassword))
            {
                result = new {data = 0, message = "密码不能为空"};
                return Json(result);
            }
            if (loginError >= 4)
            {
                if (string.IsNullOrEmpty(validateCode))
                {
                    result = new {data = 0, message = "验证码不能为空"};
                    return Json(result);
                }
                if (validateCode.ToLower() != Request.GetCookieValue("ValidateCode").ToLower())
                {
                    result = new {data = 0, message = "验证码错误"};
                    return Json(result);
                }
            }


            QueryExpression query = new QueryExpression();
            query.EntityType = typeof (AdminUser);
            query.Select(AdminUser_.AdminUserId);
            query.Select(AdminUser_.AdminUserName);
            query.Select(AdminUser_.AdminPassword);
            query.Wheres.Add(AdminUser_.AdminUserName.TEqual(adminUserName.ToLower()));
            query.Wheres.Add(AdminUser_.AdminPassword.TEqual(Encryption.GetMD5HashHex(adminPassword.ToLower())));
            AdminUsers adminUsers = ServiceManager.AdminUserService.GetUsers(query);
            if (adminUsers.Items.Count > 0)
            {
                AdminUser user = adminUsers.Items[0];
                CurrentAdminUser currentAdminUser = new CurrentAdminUser();
                currentAdminUser.AdminUserName = user.AdminUserName;
                currentAdminUser.AdminUserId = user.AdminUserId;
                Response.SetCookie(WebConfig.AdminCookieName, currentAdminUser);
                Response.RemoveCookie("LoginError");
                result = new {data = 1, message = ""};
            }
            else
            {
                Response.SetCookie("LoginError", loginError.ToString());
                result = new {data = 0, message = "用户名或密码错误"};
            }
            return Json(result);
        }

        public ActionResult ValidateCode()
        {
            ValidateCode validateCode = new ValidateCode();
            validateCode.Width = 100;
            validateCode.Height = 35;
            //生成验证码指定的长度
            string code = validateCode.GetRandomString(4);
            Response.SetCookie("ValidateCode", code);
            //创建验证码的图片
            byte[] bytes = validateCode.CreateImage(code);
            //最后将验证码返回
            return File(bytes, @"image/jpeg");
        }
    }
}